package com.pmcc.core.config;

import com.pmcc.core.config.security.JwtAuthenticationEntryPoint;
import com.pmcc.core.config.security.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@SuppressWarnings("SpringJavaAutowiringInspection")
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    // Spring会自动寻找同样类型的具体类注入，这里就是JwtUserDetailsServiceImpl了
    @Autowired
    private JwtAuthenticationEntryPoint unauthorizedHandler;

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder
                .userDetailsService(this.userDetailsService)
                .passwordEncoder(passwordEncoder());
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public JwtAuthenticationTokenFilter authenticationTokenFilterBean() throws Exception {
        return new JwtAuthenticationTokenFilter();
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                // we don't need CSRF because our token is invulnerable
                .csrf().disable()

                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()

                // don't create session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests()
                .antMatchers(
                        HttpMethod.GET,
                        "/",
                        "/user/login",
                        "/*.html",
                        "/favicon.ico",
                        "/static/**",
                        "/html/**",
                        "/**/*.woff",
                        "/**/*.ttf",
                        "/**/*.html",
                        "/**/*.*.html",
                        "/**/*.css",
                        "/**/*.js",
                        "/**/*.*.js",
                        "/websocket/**",

                        "/**/**/*.css",
                        "/**/**/*.svg",
                        "/**/**/*.png",
                        "/**/**/*.jpg",
                        "/**/**/*.gif",
                        "/**/**/*.woff",
                        "/**/**/*.woff2",
                        "/**/**/*.txt",
                        "/**/**/*.ttf"
                ).permitAll()
                .antMatchers("/v2/api-docs", "/swagger-resources/configuration/ui", "/swagger-resources/**", "/configuration/security", "/swagger-ui.html", "/webjars/**").permitAll()
                .antMatchers("/auth", "/api/v1/**").permitAll()
                .antMatchers("/common/**").permitAll()
                //小程序端接口
                .antMatchers("/app/**").permitAll()
                .antMatchers("/download/**").permitAll()
                .antMatchers("/miniProgram/index/**").permitAll()
                .antMatchers("/miniProgram/user/**").permitAll()
                .antMatchers("/api/v1/brand/**").permitAll()
                .antMatchers("/xuetang/**").permitAll()
                .antMatchers("/miniProgram/waiter/**").permitAll()
                .antMatchers("/miniProgram/admin/**").permitAll()
                .antMatchers("/miniProgram/salesman/**").permitAll()
                .antMatchers("/miniProgram/shopping/**").permitAll()
                .anyRequest().authenticated();

        // Custom JWT based security filter
        httpSecurity.addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);

        // disable page caching
        httpSecurity.headers().cacheControl();
    }
}